package org.dromara.system.runner;

import cn.hutool.core.bean.BeanUtil;
import org.dromara.common.core.constant.GlobalConstants;
import org.dromara.common.redis.utils.RedisUtils;
import org.dromara.system.domain.vo.SysUserVo;
import org.dromara.system.mapper.SysUserMapper;
import org.dromara.system.service.ISysOssConfigService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 初始化 system 模块对应业务数据
 *
 * @author Hao Tong Xue
 */
@Slf4j
@RequiredArgsConstructor
@Component
public class SystemApplicationRunner implements ApplicationRunner {

    private final ISysOssConfigService ossConfigService;

    private final SysUserMapper userMapper;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        ossConfigService.init();
        log.info("初始化OSS配置成功");
        cacheUserInfo();
        log.info("初始化用户信息成功");

    }

    private void cacheUserInfo(){

        List<SysUserVo> userList = userMapper.selectVoList();
        for (SysUserVo sysUserVo : userList) {
            RedisUtils.setCacheObject(GlobalConstants.USER_INFO_KEY + sysUserVo.getUserId(), BeanUtil.beanToMap(sysUserVo));
        }

    }
}
